import Toast from 'tdesign-miniprogram/toast/index';
import {
  getContactDetail,
  editContactor,
  addNewContactor
} from '~/services/api/menber'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isEdit: false,
    showSaveBtn: false,
    kid: "",
    genderOPtions: [{
        value: 0,
        label: '未知'
      },
      {
        value: 1,
        label: '男'
      },
      {
        value: 2,
        label: '女'
      }
    ],
    materialOPtions: [{
        value: 0,
        label: '未婚'
      },
      {
        value: 1,
        label: '已婚'
      },
    ],
    personInfo: {
      age: "",
      agentUserId: "",
      duty: "",
      gender: '',
      kid: "",
      maritalStatus: '',
      memo: "",
      userName: "",
      userPhone: ""
    },
  },
  onChangeRadio(event) {
    console.log(event);
    const {
      value
    } = event.detail;
    const key = event.currentTarget.dataset.key;
    console.log(key);
    this.setData({
      [key]: value
    });
  },

  onInputChange(e) {
    const key = e.currentTarget.dataset.key,
      value = e.detail.value;
    this.setData({
      [key]: value
    })
  },
  editStatus() {
    this.setData({
      isEdit: false,
      showSaveBtn: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var options = currentPage.options //如果要获取url中所带的参数可以查看options
    if (options.isEdit) {
      this.getDetailData(options)
      wx.setNavigationBarTitle({
        title: '联系人详情',
      })
    } else {
      this.setData({
        showSaveBtn: true,
        agentId: options.agentId
      })
      wx.setNavigationBarTitle({
        title: '添加联系人',
      })
    }
  },

  onChange(e) {
    this.data.userList.forEach((el) => {
      if (el.key == e.detail.value) {
        this.setData({
          popvisible: false,
          pid: e.detail.value,
          upperAgent: el.value
        })
      }
    })

  },
  async getDetailData(options) {
    let {
      code,
      data
    } = await getContactDetail({
      kid: options.kid
    });
    this.setData({
      isEdit: options.isEdit,
      personInfo: data,
      kid: options.kid,
      agentId: options.agentId
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  save() {
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var options = currentPage.options //如果要获取url中所带的参数可以查看options
    let patten = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!this.data.personInfo.userName ||
      !this.data.personInfo.gender ||
      !this.data.personInfo.maritalStatus ||
      !this.data.personInfo.age
    ) {
      Toast({
        selector: '#t-toast',
        message: '请填写联系人姓名',
        icon: '',
        duration: 1000,
      });
      return;
    };
    if (!patten.test(this.data.personInfo.userPhone)) {
      Toast({
        selector: '#t-toast',
        message: '请填写正确的手机号码',
        icon: '',
        duration: 1000,
      });
      return;
    };
    if (!this.data.personInfo.age || this.data.personInfo.age < 10) {
      Toast({
        selector: '#t-toast',
        message: '年龄不能小于10岁',
        icon: '',
        duration: 1000,
      });
      return;
    };
    let params = {
      ...this.data.personInfo,
    };
    console.log(params);
    if (options.isEdit) {
      //  编辑
      this.editData(params)
    } else {
      // 新增
      this.saveData(params)
    }
  },
  async editData(params) {
    let res = await editContactor({
      ...params,
      kid: this.data.kid
    });
    if (res.code == '200') {
      wx.redirectTo({
        url: `/subpages/pages/contactor/contactor?agentUserId=${this.data.agentId}`,
      })
    }
  },

  async saveData(params) {
    let res = await addNewContactor({
      ...params,
      agentUserId: this.data.agentId

    });
    if (res.code == '200') {
      wx.redirectTo({
        url: `/subpages/pages/contactor/contactor?agentUserId=${this.data.agentId}`,
      })
    }
  },
})